package com.example.myapplication;

import android.os.Bundle;
import android.view.OrientationEventListener;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;


import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.androiduidemo.R;
import com.example.myapplication.javabean.Courses;
import com.example.myapplication.javabean.User;

public class AddClass extends AppCompatActivity {
    private MySQLiteOpenHelper1 mySQLiteOpenHelper;
    private EditText medcourseName,medteacherName,mtime,mlocatil;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Compiler EdgeToEdge = null;
        EdgeToEdge.enable();
        setContentView(R.layout.activity_add_class);
        mySQLiteOpenHelper = new MySQLiteOpenHelper1(this);
        find();
        Courses courses = new Courses();

    }
    public  void find()
    {
        medcourseName=findViewById(R.id.ed_courseName);
        medteacherName=findViewById(R.id.ed_teacherName);
        mtime=findViewById(R.id.ed_Time);
        mlocatil=findViewById(R.id.ed_location);
    }



    public void add_class(View view) {
        // 获取输入框的值
        String s1 = medcourseName.getText().toString();
        String s2 = medteacherName.getText().toString();
        String s3 = mtime.getText().toString();
        String s4 = mlocatil.getText().toString();


        // 创建Courses对象
        Courses cour = new Courses(s1, s2, s3, s4);

        // 使用AlertDialog询问用户是否确认添加
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("确认添加")
                .setMessage("您确定要添加这门课程吗？")
                .setPositiveButton("确认", (dialog, which) -> {
                    // 用户点击确认，执行添加操作
                    long register = mySQLiteOpenHelper.register(cour);
                    if (register != -1) {
                        Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "添加失败，可能用户名被占用或出现其他错误", Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", (dialog, which) -> {
                    // 用户点击取消，不执行任何操作，对话框关闭
                    dialog.cancel();
                })
                .create()
                .show();
    }
    }
